package com.yzh.wanandroid.network.interceptor


import com.yzh.wanandroid.utils.*
import okhttp3.Interceptor
import okhttp3.Response

/**
 * 设置请求头,查找登录信息
 */
class HeaderInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {

        val request = chain.request()
        val builder = request.newBuilder()

        builder.addHeader("Content-type", "application/json; charset=utf-8")
        val host = request.url.host
        val url = request.url.toString()
        if (host.isNotEmpty() && (url.contains(COLLECTIONS_WEBSITE)
                        || url.contains(UNCOLLECTIONS_WEBSITE)
                        || url.contains(ARTICLE_WEBSITE)
                        || url.contains(TODO_WEBSITE)
                        || url.contains(COIN_WEBSITE))) {
            val spHost = SpUtils.getString(host,"").toString()
            val cookie: String = if (spHost.isNotEmpty()) spHost else ""
            if (cookie.isNotEmpty()) {
                builder.addHeader(COOKIE_NAME, cookie)
            }
        }
        return chain.proceed(builder.build())
    }
}